package com.samphanie.auiu.biz_home.logic;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentManager;

import com.samphanie.auiu.biz_home.R;
import com.samphanie.auiu.common.tab.AuiuFragmentTabView;
import com.samphanie.auiu.common.tab.AuiuTabViewAdapter;
import com.samphanie.auiu.biz_home.fragment.FavoriteFragment;
import com.samphanie.auiu.biz_home.fragment.ProfileFragment;
import com.samphanie.auiu.biz_home.fragment.RecommendFragment;
import com.samphanie.auiu.biz_home.fragment.category.CategoryFragment;
import com.samphanie.auiu.biz_home.fragment.home.HomePageFragment;
import com.samphanie.auiu.library.utils.AuiuDisplayUtil;
import com.samphanie.auiu.ui.tab.bottom.AuiuTabBottom;
import com.samphanie.auiu.ui.tab.bottom.AuiuTabBottomInfo;
import com.samphanie.auiu.ui.tab.bottom.AuiuTabBottomLayout;
import com.samphanie.auiu.ui.tab.common.IAuiuTabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZSY
 */
public class MainActivityLogic {

    private AuiuFragmentTabView fragmentTabView;
    private AuiuTabBottomLayout auiuTabBottomLayout;
    private List<AuiuTabBottomInfo<?>> infoList;
    private ActivityProvider activityProvider;
    private final static String SAVED_CURRENT_ID = "SAVED_CURRENT_ID";
    private int currentItemIndex;

    public MainActivityLogic(ActivityProvider activityProvider, @Nullable Bundle savedInstanceState) {
        this.activityProvider = activityProvider;
        //fix 不保留活动导致的Fragment重叠问题
        if (savedInstanceState != null) {
            currentItemIndex = savedInstanceState.getInt(SAVED_CURRENT_ID);
        }
        initTabBottom();
    }

    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putInt(SAVED_CURRENT_ID, currentItemIndex);
    }

    private void initTabBottom() {
        auiuTabBottomLayout = activityProvider.findViewById(R.id.tab_bottom_layout);
        auiuTabBottomLayout.setAlpha(0.85f);
        infoList = new ArrayList<>();

        int defaultColor = activityProvider.getResources().getColor(R.color.tabBottomDefaultColor);
        int tintColor = activityProvider.getResources().getColor(R.color.tabBottomTintColor);

        AuiuTabBottomInfo<Integer> homeInfo = new AuiuTabBottomInfo<>(
                "首页",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_home),
                null,
                defaultColor,
                tintColor
        );
        homeInfo.fragment = HomePageFragment.class;

        AuiuTabBottomInfo<Integer> favoriteInfo = new AuiuTabBottomInfo<>(
                "收藏",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_favorite),
                null,
                defaultColor,
                tintColor
        );
        favoriteInfo.fragment = FavoriteFragment.class;

//        AuiuTabBottomInfo<Integer> categoryInfo = new AuiuTabBottomInfo<>(
//                "分类",
//                "font/iconfont.ttf",
//                activityProvider.getString(R.string.if_category),
//                null,
//                defaultColor,
//                tintColor
//        );
        Bitmap icon = BitmapFactory.decodeResource(activityProvider.getResources(), R.drawable.jiahao, null);
        Bitmap selectIcon = BitmapFactory.decodeResource(activityProvider.getResources(), R.drawable.select_jiahao, null);
        AuiuTabBottomInfo<String> categoryInfo = new AuiuTabBottomInfo<>(
                "分类",
                icon,
                selectIcon
        );
        categoryInfo.fragment = CategoryFragment.class;

        AuiuTabBottomInfo<Integer> recommendInfo = new AuiuTabBottomInfo<>(
                "推荐",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_recommend),
                null,
                defaultColor,
                tintColor
        );
        recommendInfo.fragment = RecommendFragment.class;

        AuiuTabBottomInfo<Integer> profileInfo = new AuiuTabBottomInfo<>(
                "我的",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_profile),
                null,
                defaultColor,
                tintColor
        );
        profileInfo.fragment = ProfileFragment.class;
        infoList.add(homeInfo);
        infoList.add(favoriteInfo);
        infoList.add(categoryInfo);
        infoList.add(recommendInfo);
        infoList.add(profileInfo);
        auiuTabBottomLayout.inflateInfo(infoList);

        initFragmentTabView();

        auiuTabBottomLayout.addTabSelectedChangeListener(new IAuiuTabLayout.OnTabSelectedListener<AuiuTabBottomInfo<?>>() {
            @Override
            public void onTabSelectedChange(int index, @Nullable AuiuTabBottomInfo<?> prevInfo, @NonNull AuiuTabBottomInfo<?> nextInfo) {
                fragmentTabView.setCurrentItem(index);
                MainActivityLogic.this.currentItemIndex = index;
            }
        });
        auiuTabBottomLayout.defaultSelected(infoList.get(currentItemIndex));

        // 自定义tab的高度
        AuiuTabBottom tab = auiuTabBottomLayout.findTab(infoList.get(2));
        tab.resetHeight(AuiuDisplayUtil.dp2px(46f, activityProvider.getResources()));
        // 自定义显示tab的名字
        tab.showTabNameView(false);
    }

    private void initFragmentTabView() {
        AuiuTabViewAdapter tabViewAdapter  =new AuiuTabViewAdapter(activityProvider.getSupportFragmentManager(), infoList);
        fragmentTabView = activityProvider.findViewById(R.id.fragment_tab_view);
        fragmentTabView.setAdapter(tabViewAdapter);
    }

    public interface ActivityProvider {
        <T extends View> T findViewById(@IdRes int id);

        Resources getResources();

        FragmentManager getSupportFragmentManager();

        String getString(@StringRes int resId);
    }

    public AuiuFragmentTabView getFragmentTabView() {
        return fragmentTabView;
    }

    public AuiuTabBottomLayout getAuiuTabBottomLayout() {
        return auiuTabBottomLayout;
    }

    public List<AuiuTabBottomInfo<?>> getInfoList() {
        return infoList;
    }
}
